<template>
  <view class="container">
    <view class="header">
      <view class="title">设置</view>
      <view class="subtitle">个性化配置</view>
    </view>

    <view class="settings-container">
      <!-- 默认设置 -->
      <view class="setting-section">
        <view class="section-title">
          <text>默认设置</text>
        </view>

        <view class="setting-item">
          <text class="setting-label">速判练习默认题数</text>
          <uni-number-box
              :max="10"
              :min="3"
              :value="settings.speedJudgeQuestionCount"
              @change="updateSetting('speedJudgeQuestionCount', $event)"
          ></uni-number-box>
        </view>

        <view class="setting-item">
          <text class="setting-label">速判选择默认题数</text>
          <uni-number-box
              :max="20"
              :min="5"
              :value="settings.speedJudgeChoiceQuestionCount"
              @change="updateSetting('speedJudgeChoiceQuestionCount', $event)"
          ></uni-number-box>
        </view>

        <view class="setting-item">
          <text class="setting-label">速算练习默认题数</text>
          <uni-number-box
              :max="50"
              :min="5"
              :value="settings.speedCalcQuestionCount"
              @change="updateSetting('speedCalcQuestionCount', $event)"
          ></uni-number-box>
        </view>

        <view class="setting-item">
          <text class="setting-label">速算选择默认题数</text>
          <uni-number-box
              :max="30"
              :min="5"
              :value="settings.speedCalcChoiceQuestionCount"
              @change="updateSetting('speedCalcChoiceQuestionCount', $event)"
          ></uni-number-box>
        </view>

        <view class="setting-item">
          <text class="setting-label">公式速判默认题数</text>
          <uni-number-box
              :max="20"
              :min="5"
              :value="settings.speedFormulaChoiceQuestionCount"
              @change="updateSetting('speedFormulaChoiceQuestionCount', $event)"
          ></uni-number-box>
        </view>
      </view>

      <!-- 数据管理 -->
      <view class="setting-section">
        <view class="section-title">
          <uni-icons color="#007AFF" size="20" type="folder"></uni-icons>
          <text>数据管理</text>
        </view>

        <view class="setting-item" @tap="clearData">
          <view class="setting-info">
            <text class="setting-label">清除所有数据</text>
            <text class="setting-desc">清除所有练习记录和设置</text>
          </view>
          <uni-icons color="#ccc" size="16" type="right"></uni-icons>
        </view>
      </view>

      <!-- 关于应用 -->
      <view class="setting-section">
        <view class="section-title">
          <text>关于应用</text>
        </view>

        <view class="setting-item">
          <text class="setting-label">应用版本</text>
          <text class="setting-value">1.0.0</text>
        </view>

        <view class="setting-item" @tap="showAbout">
          <view class="setting-info">
            <text class="setting-label">关于速算练习</text>
            <text class="setting-desc">应用介绍和使用说明</text>
          </view>
          <uni-icons color="#ccc" size="16" type="right"></uni-icons>
        </view>
      </view>

      <!-- 统计信息 -->
      <view v-if="stats" class="setting-section">
        <view class="section-title">
          <uni-icons color="#007AFF" size="20" type="chart"></uni-icons>
          <text>统计信息</text>
        </view>

        <view class="stats-grid">
          <view class="stat-item">
            <text class="stat-value">{{ stats.totalPractices }}</text>
            <text class="stat-label">总练习次数</text>
          </view>
          <view class="stat-item">
            <text class="stat-value">{{ stats.totalQuestions }}</text>
            <text class="stat-label">总答题数</text>
          </view>
          <view class="stat-item">
            <text class="stat-value">{{ stats.totalTime }}</text>
            <text class="stat-label">总练习时间</text>
          </view>
          <view class="stat-item">
            <text class="stat-value">{{ stats.averageAccuracy }}%</text>
            <text class="stat-label">平均正确率</text>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import Storage from '@/utils/storage.js';

export default {
  data() {
    return {
      settings: {
        speedJudgeQuestionCount: 5,
        speedCalcQuestionCount: 10,
        speedCalcChoiceQuestionCount: 10,
        speedFormulaChoiceQuestionCount: 10,
        speedJudgeChoiceQuestionCount: 10
      },
      stats: null
    }
  },
  onLoad() {
    this.loadSettings();
    this.loadStats();
  },
  methods: {
    // 加载设置
    loadSettings() {
      this.settings = Storage.getUserSettings();
    },

    // 更新设置
    updateSetting(key, value) {
      this.settings[key] = value;
      Storage.saveUserSettings(this.settings);
      uni.showToast({
        title: '设置已保存',
        icon: 'success'
      });
    },

    // 加载统计信息
    loadStats() {
      const speedCalcRecords = Storage.getSpeedCalcRecords();
      const speedJudgeRecords = Storage.get('speed_judge_records', []);

      if (speedCalcRecords.length === 0 && speedJudgeRecords.length === 0) {
        this.stats = null;
        return;
      }

      const totalPractices = speedCalcRecords.length + speedJudgeRecords.length;
      const totalQuestions = speedCalcRecords.reduce((sum, r) => sum + r.questionCount, 0) +
          speedJudgeRecords.reduce((sum, r) => sum + r.questionCount, 0);

      const totalTimeMinutes = Math.round(
          (speedCalcRecords.reduce((sum, r) => sum + r.totalTime, 0) +
              speedJudgeRecords.reduce((sum, r) => sum + r.practiceTime, 0)) / 60
      );

      const allAccuracies = [
        ...speedCalcRecords.map(r => r.accuracy),
        ...speedJudgeRecords.map(r => r.accuracy)
      ];
      const averageAccuracy = allAccuracies.length > 0 ?
          Math.round(allAccuracies.reduce((sum, acc) => sum + acc, 0) / allAccuracies.length) : 0;

      this.stats = {
        totalPractices,
        totalQuestions,
        totalTime: `${totalTimeMinutes}分钟`,
        averageAccuracy
      };
    },

    // 清除所有数据
    clearData() {
      uni.showModal({
        title: '确认清除',
        content: '这将删除所有练习记录和设置，此操作不可恢复！',
        confirmText: '确认清除',
        confirmColor: '#dc3545',
        success: (res) => {
          if (res.confirm) {
            try {
              Storage.clear();
              uni.showToast({
                title: '数据已清除',
                icon: 'success'
              });

              // 重新加载设置和统计
              this.loadSettings();
              this.loadStats();
            } catch (e) {
              uni.showToast({
                title: '清除失败',
                icon: 'error'
              });
            }
          }
        }
      });
    },

    // 显示关于信息
    showAbout() {
      uni.showModal({
        title: '关于速算练习',
        content: `速算练习 v1.0.0 一款专注于数学速算和速判能力提升的小程序。`,
        showCancel: false
      });
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.header {
  background: linear-gradient(135deg, #a8f3f0 0%, #7dd3fc 100%);
  padding: 60rpx 40rpx 40rpx;
  color: white;
  text-align: center;

  .title {
    font-size: 40rpx;
    font-weight: bold;
    margin-bottom: 15rpx;
  }

  .subtitle {
    font-size: 26rpx;
    opacity: 0.9;
  }
}

.settings-container {
  padding: 40rpx;
}

.setting-section {
  background: white;
  border-radius: 15rpx;
  padding: 30rpx;
  margin-bottom: 30rpx;
  box-shadow: 0 5rpx 15rpx rgba(0, 0, 0, 0.05);

  .section-title {
    display: flex;
    align-items: center;
    margin-bottom: 25rpx;

    text {
      font-size: 30rpx;
      font-weight: bold;
      margin-left: 15rpx;
      color: #333;
    }
  }
}

.setting-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 25rpx 0;
  border-bottom: 1rpx solid #f0f0f0;

  &:last-child {
    border-bottom: none;
  }

  .setting-label {
    font-size: 28rpx;
    color: #333;
  }

  .setting-value {
    font-size: 26rpx;
    color: #666;
  }

  .setting-info {
    flex: 1;

    .setting-label {
      display: block;
      font-size: 28rpx;
      color: #333;
      margin-bottom: 5rpx;
    }

    .setting-desc {
      font-size: 24rpx;
      color: #666;
    }
  }
}

.stats-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 20rpx;

  .stat-item {
    flex: 1;
    min-width: 45%;
    text-align: center;
    padding: 25rpx;
    background: #f8f9fa;
    border-radius: 10rpx;

    .stat-value {
      display: block;
      font-size: 32rpx;
      font-weight: bold;
      color: #007AFF;
      margin-bottom: 8rpx;
    }

    .stat-label {
      font-size: 24rpx;
      color: #666;
    }
  }
}
</style>